package com.example.my_flea_street.controller;

import com.example.my_flea_street.bean.Info;
import com.example.my_flea_street.bean.InfoPage;
import com.example.my_flea_street.bean.ResponseModel;
import com.example.my_flea_street.service.impl.CategoryServiceImpl;
import com.example.my_flea_street.util.ResponseHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value = "/category")
public class CategoryController {

    private final CategoryServiceImpl categoryService;

    @Autowired
    public CategoryController(CategoryServiceImpl categoryService) {
        this.categoryService = categoryService;
    }
    @RequestMapping(value = "/getInfoByType", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> getInfoByType(@RequestBody Map<String, String> json ) {
        int category = Integer.parseInt(json.get("category"));
        int pageNum = Integer.parseInt(json.get("page_no"));
        int pageSize = Integer.parseInt(json.get("page_size"));
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Info> pageInfo = new PageInfo<>(categoryService.getInfo(category));
        InfoPage infoPage = new InfoPage();
        infoPage.setTotal((int)pageInfo.getTotal());
        infoPage.setPages(pageInfo.getPages());
        infoPage.setPageNum(pageInfo.getPageNum());
        infoPage.setData(pageInfo.getList());
        return ResponseHelper.createResponse(0, "成功",infoPage);
    }

}
